package com.shangcheng.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.PmsProductApi;
import com.shangcheng.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.shangcheng.model.domain.PmsProduct;
import com.shangcheng.model.domain.SmsFlashPromotionProductRelation;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.SmsFlashPromotionProductRelationVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionProductRelationService {

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    @DubboReference
    private PmsProductApi pmsProductApi;

    /**
     * 添加活动和产品关系
     *
     * @param relationList
     */
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        smsFlashPromotionProductRelationApi.save(relationList);
    }

    /**
     * 删除活动和产品关系
     *
     * @param id
     */
    public void delete(Integer id) {
        smsFlashPromotionProductRelationApi.delete(id);
    }

    /**
     * 修改活动和产品关系
     *
     * @param id
     * @param relation
     */
    public void update(Integer id, SmsFlashPromotionProductRelation relation) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = this.findById(id);
        if (ObjectUtil.isEmpty(smsFlashPromotionProductRelation)) return;
        relation.setId(smsFlashPromotionProductRelation.getId());
        smsFlashPromotionProductRelationApi.update(relation);
    }

    /**
     * 通过id获取活动和产品关系
     *
     * @param id
     * @return
     */
    public SmsFlashPromotionProductRelation findById(Integer id) {
        return smsFlashPromotionProductRelationApi.findById(id);
    }

    /**
     * 分页条件查询
     *
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResult findList(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        //1.利用分页查询所在秒杀活动和转场活动中的分页
        IPage page = smsFlashPromotionProductRelationApi.findList(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        //2.得到smsFlashPromotionProductRelation集合
        List<SmsFlashPromotionProductRelation> records = page.getRecords();
        //3.利用copy算法进行商品id复制
        List<SmsFlashPromotionProductRelationVo> list = new ArrayList<>();
        //3.productIds遍历封装smsFlashPromotionProductRelationVo
        for (SmsFlashPromotionProductRelation relation : records) {
            //根据id进行商品的获取
            PmsProduct pmsProduct = pmsProductApi.findById(relation.getProductId());
            //将SmsFlashPromotionProductRelation的属性copy给SmsFlashPromotionProductRelationVo
            SmsFlashPromotionProductRelationVo vo = SmsFlashPromotionProductRelationVo.init(relation, pmsProduct);
            list.add(vo);
        }
        //4.封装PageResult
        PageResult pr = new PageResult();
        pr.setList(list);
        pr.setTotal(Convert.toInt(page.getTotal()));
        pr.setPageNum(pageNum);
        pr.setPageSize(pageSize);
        pr.setTotalPage(Convert.toInt(page.getPages()));
        return pr;
    }
}
